package com.lz.specification.concurrent;

import java.util.concurrent.TimeUnit;

/**
 * what...
 * how...
 * why...
 *
 * @author: li
 * @date: 2019/1/17
 */

public class ThreadLocalDemo02 {
    private static final StringBuilder INIT_VALUE = new StringBuilder("init");

    private static final ThreadLocal<StringBuilder> builder = new ThreadLocal<StringBuilder>() {
        @Override
        protected StringBuilder initialValue() {
            return new StringBuilder("init");
        }
    };


    private static class AppendStringThread extends Thread {
        @Override
        public void run() {
            StringBuilder inThread = builder.get();
            for (int i = 0; i < 10; i++) {
                inThread.append("-" + i);
            }

            System.out.println(getName() + " : " + inThread.toString() );
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            AppendStringThread appendStringThread = new AppendStringThread();
            appendStringThread.start();

        TimeUnit.SECONDS.sleep(3);
            System.out.println();
        }
        TimeUnit.SECONDS.sleep(10);

    }
}
